package com.cary.web;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends AppCompatActivity implements View.OnClickListener {
    private String TAG = MainActivity.class.getSimpleName();

    private TextView tvStart;
    private TextView tvEnd;
    private TextView tvDesc;
    private TextView tvReadNum;
    private TextView etUrl;

    private int life = 0;
    private String webUrl = "https://www.hdb.com/party/p2woa.html";
    private Timer timer;
    private boolean isStart = false;

    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.activity_main);

        tvStart = findViewById(R.id.tv_start);
        tvEnd = findViewById(R.id.tv_end);
        tvDesc = findViewById(R.id.tv_desc);
        tvReadNum = findViewById(R.id.tv_read_num);
        etUrl = findViewById(R.id.et_url);

        tvStart.setOnClickListener(this);
        tvEnd.setOnClickListener(this);


    }

    @Override
    public void onClick(View view) {
        if (view == tvStart) {
            webUrl = etUrl.getText().toString().trim();
            if(TextUtils.isEmpty(webUrl)){
                webUrl = "https://www.hdb.com/party/p2woa.html";
            }
            Log.d(TAG, "跳转webview");
            Toast.makeText(this, "start 阅读", Toast.LENGTH_SHORT).show();
            tvDesc.setText("测试中，请勿动\n 结束测试请点击End按钮");
            startTimer();
            isStart = true;
        } else if (view == tvEnd) {
            if (isStart) {
                Log.d(TAG, "结束Webview");
                Toast.makeText(this, "end 阅读", Toast.LENGTH_SHORT).show();
                tvDesc.setText("已结束测试\n 重新测试请点击Start按钮");
                endIntent();
            } else {
                Toast.makeText(this, "请先点击Start", Toast.LENGTH_SHORT).show();
            }
        }
    }

    private void startIntent() {
        Intent intent = new Intent(this, WebViewActivity.class);
        intent.putExtra("webUrl", webUrl);
        startActivityForResult(intent, 1000);
    }

    private void endIntent() {
        stopTimer();
    }

    private void startTimer() {
        if (timer != null) {
            stopTimer();
        }
        timer = new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                startIntent();
            }
        }, 3000);
    }

    private void stopTimer() {
        if (timer != null) {
            timer.cancel();
            timer = null;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        try {
            Log.d(TAG, "requestCode=" + requestCode + ", resultCode=" + resultCode + ", readNum=" + data.getIntExtra("readNum", 0));
            if (requestCode == 1000 && resultCode == 1000) {
                life++;
                tvReadNum.setText("已经阅读" + life + "次");
                startTimer();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
